//
//  CPSCAll2CategoryRightView.m
//  CollTest
//
//  Created by codew on 11/10/21.
//

#import "CPSCAll2CategoryRightView.h"
#import "CPSCAll2CategoryRightBannerView.h"
#import "CPSCAll2CategoryRightITBTagCell.h"
#import "CPSCAll2CategoryRightRecommendCell.h"
#import "CPSCAll2CategoryRightViewUIModel.h"
#import "CPSCAll2CategoryRightTitleHeaderView.h"



@interface CPSCAll2CategoryRightView ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
//@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) CPSCAll2CategoryRightBannerView * viewHeaderBanner;

@property (nonatomic, strong) CPSCAll2CategoryRightViewUIModel *modelUI;
@end

/** 推荐 */
static NSString * RecommendCellIdentifier = @"CPSCAll2CategoryRightRecommendCell";
/** 标签 */
static NSString * CPSCAll2CategoryRightITBTagCellIdentifier = @"CPSCAll2CategoryRightITBTagCell";
/** 头 */
static NSString * TitleHeaderViewIdentifier = @"CPSCAll2CategoryRightTitleHeaderView";

@implementation CPSCAll2CategoryRightView

+ (instancetype)viewRight
{
    CPSCAll2CategoryRightView *view = [[[NSBundle mainBundle] loadNibNamed:@"CPSCAll2CategoryRightView" owner:nil options:nil] firstObject];
    return view;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self setUpMainUI];
    [self deBugUI];
}

- (void)setUpMainUI
{
    
    DggWEAKSELF(weakSelf);;
    
    MJRefreshStateHeader * header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        
        if (weakSelf.blockSwitch) {
            
            weakSelf.blockSwitch(CPSCAll2CategoryViewSwitchType_Back);
        }
        
        [weakSelf resetView];
        [weakSelf.tableView.mj_header endRefreshing];
        
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    self.tableView.mj_header = header;

//    stateLabel
    MJRefreshBackFooter * footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        
        if (weakSelf.blockSwitch) {
            
            weakSelf.blockSwitch(CPSCAll2CategoryViewSwitchType_Next);
        }
        
        [weakSelf resetView];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
    
}

- (void)deBugUI
{
    
//    self.viewHeader.backgroundColor = UIColor.redColor;
}

- (void)resetView
{
    self.tableView.hidden = YES;
    
//    if (self.modelUI.countSection > 0) {
//
//        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
//                              atScrollPosition:UITableViewScrollPositionTop
//                                      animated:NO];
//    }else{
//        [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
//    }
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
}

- (void)updateViewWithItem:(CPSCAll2CategoryRightViewUIModel *)modelUI
{
    self.tableView.hidden = NO;
    self.modelUI = modelUI;
    [self.tableView reloadData];
}

#pragma mark - Getter Setter

- (CPSCAll2CategoryRightBannerView *)viewHeaderBanner
{
    
    if (!_viewHeaderBanner) {
        
        CPSCAll2CategoryRightBannerView * view = [CPSCAll2CategoryRightBannerView viewBanner];
        view.backgroundColor = [UIColor whiteColor];
                
        _viewHeaderBanner = view;
    }
    return _viewHeaderBanner;
}


#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger count = self.modelUI.countSection;
    return count;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    BOOL hasBanner = self.modelUI.dataArrayBanner.count > 0;
    
    // 有banner
    if (section == 0 && hasBanner) {

        // 跟新banner
        return self.viewHeaderBanner;
    }else{
        
        CPSCAll2CategoryRightTitleHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TitleHeaderViewIdentifier];
        if (header == nil) {
            header = [[CPSCAll2CategoryRightTitleHeaderView alloc] initWithReuseIdentifier:TitleHeaderViewIdentifier];
        }
                
        NSInteger indexValue = hasBanner ? 1 : 0;
        if (section == indexValue && self.modelUI.dataArrayRecommend.count > 0) {
            
            [header updateViewWithTitle:@"推荐分类"];
        }else{
            
            NSInteger indexValueC = section - self.modelUI.countExceptSecond;
            id<CPSCAll2CategoryRightViewUIModelProtocol> sectionItem = self.modelUI.dataArraySecond[indexValueC];
            [header updateViewWithTitle:sectionItem.stringTitleSectionSecond];
        }        
        
        return header;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    BOOL hasBanner = self.modelUI.dataArrayBanner.count > 0;
    
    if (section == 0 && hasBanner) {
        // 0. banner
        return 136;
    }else{
        // 推荐 + 二级分类
        return 50;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    BOOL hasBanner = self.modelUI.dataArrayBanner.count > 0;
    
    if (section == 0 && hasBanner) {
        // 0. banner
        return 0;
    }else{
        // 推荐 + 二级分类
        return 1;
    }
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BOOL hasBanner = self.modelUI.dataArrayBanner.count > 0;
    NSInteger indexValue = hasBanner ? 1 : 0;
    if (indexPath.section == indexValue && self.modelUI.dataArrayRecommend.count > 0) {
        
        // 1. 推荐
        
        CPSCAll2CategoryRightRecommendCell  * cell = [tableView dequeueReusableCellWithIdentifier:RecommendCellIdentifier];
        
        if (!cell) {
            
            [tableView registerNib:[UINib nibWithNibName:RecommendCellIdentifier bundle:nil] forCellReuseIdentifier:RecommendCellIdentifier];
            cell = [tableView dequeueReusableCellWithIdentifier:RecommendCellIdentifier];
        }
        
        [cell updateCellWithItems:self.modelUI.dataArrayRecommend];
        
        DggWEAKSELF(weakSelf);
        cell.blockSelectRecommend = ^(id<CPSCAll2CategoryRightViewUIModelProtocol>  _Nonnull dataItem) {
                      
            if (weakSelf.blockSelectRecommend) {
                
                weakSelf.blockSelectRecommend(dataItem);
            }
        };
        
        return cell;
    }else{
        
        
        // 2. 二级分类
        CPSCAll2CategoryRightITBTagCell  * cell = [tableView dequeueReusableCellWithIdentifier:CPSCAll2CategoryRightITBTagCellIdentifier];
        
        if (!cell) {
            
            [tableView registerNib:[UINib nibWithNibName:CPSCAll2CategoryRightITBTagCellIdentifier bundle:nil] forCellReuseIdentifier:CPSCAll2CategoryRightITBTagCellIdentifier];
            cell = [tableView dequeueReusableCellWithIdentifier:CPSCAll2CategoryRightITBTagCellIdentifier];
        }
        NSInteger indexValueC = indexPath.section - self.modelUI.countExceptSecond;
        id<CPSCAll2CategoryRightViewUIModelProtocol> sectionItem = self.modelUI.dataArraySecond[indexValueC];
        CGFloat cellHeight = [cell updateCellWithItem:sectionItem];
        
        [sectionItem updateCellHeight:cellHeight];
        DggWEAKSELF(weakSelf);
        cell.blockCategory = ^(id<CPSCAll2CategoryRightViewUIModelProtocol>  _Nonnull dataItem) {
          
            if (weakSelf.blockCategory) {
                
                weakSelf.blockCategory(dataItem);
            }
        };
        
        return cell;
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat heightCell;
    
    BOOL hasBanner = self.modelUI.dataArrayBanner.count > 0;
    NSInteger indexValue = hasBanner ? 1 : 0;
    if (indexPath.section == indexValue && self.modelUI.dataArrayRecommend.count > 0) {
        
        // 1. 推荐
        heightCell = [CPSCAll2CategoryRightRecommendCell heightCellDataArrayCount:self.modelUI.dataArrayRecommend.count];
        
    }else{
        
        NSInteger indexValueC = indexPath.section - self.modelUI.countExceptSecond;
        id<CPSCAll2CategoryRightViewUIModelProtocol> sectionItem = self.modelUI.dataArraySecond[indexValueC];
        
        // 2. 二级分类
        heightCell = sectionItem.heighCell ? sectionItem.heighCell : 500;
        CGFloat marginLastBottom = 16;
        heightCell += marginLastBottom;
        
        if (sectionItem.dataItemsSecond.count == 0) {
            heightCell = 0;
        }
    }
    
    
    return heightCell;
}

@end
